﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    /// <summary>
    /// WSDL帮助类
    /// </summary>
    /// <remarks>2014-5-9 by sutroon</remarks>
    public class FormWSDLHelper
    {
        /// <summary>
        /// 执行分页
        /// </summary>
        /// <param name="title"></param>
        /// <param name="args"></param>
        /// <param name="pager"></param>
        /// <param name="succussAct"></param>
        /// <param name="pageId"></param>
        /// <param name="pageSize"></param>
        /// <example>
        /// <![CDATA[
        /// int c = 0, d = 0;
        /// object[] args = new object[] { AppInfo.ConnectionName, "from tCallEnjoyList where {0}".Fmt(tvSql), "LastTime desc", "CustomerSID,AgentName,Telphone", a, b, c, d };
        /// FormWSDLHelper.ExecPaging("读取通话记录", args, formPagger2, dt =>{ dgvList.DataSource=dt; }, a, b);
        /// ]]>
        /// </example>
        public static void ExecPaging(string title, object[] args, CustomControls.Pagger.SoPagger pager, Action<DataTable> succussAct, int pageId, int pageSize)
        {
            int recordCount, pageCount;
            TaskHelper<DataTable> tskh = new TaskHelper<DataTable>();
            tskh.ShowLoading(new CustomControls.SoLoadingForm() { Text = string.Format("正在{0}...", title) });
            tskh.NewTask(() =>
            {
                DataTable dt = WSDLHelper.InvokeMethod<DataTable>("ExecutePagingDataTable", args);
                if (dt == null) MessageBox.Show(WSDLHelper.GetExceptionMessage(title));
                return dt;
            });
            tskh.ContinueWith(() =>
            {
                recordCount = (int)args[6];
                pageCount = (int)args[7];
                if (succussAct != null) succussAct(tskh.Task.Result);
                pager.Config(recordCount, pageCount, pageId, pageSize);
            });
            tskh.Start();
        }
        public static void SelectTableAction(string title, string connName, string tblName, string search, string fields, Action<DataTable> succussAct)
        {
            TaskHelper<DataTable> tskh = new TaskHelper<DataTable>();
            tskh.ShowLoading(new CustomControls.SoLoadingForm() { Text = string.Format("正在{0}...", title) });
            tskh.NewTask(() =>
            {
                DataTable dt = WSDLHelper.InvokeMethod<DataTable>("SelectTable", new object[] { connName, tblName, search, fields });
                if (dt == null) MessageBox.Show(WSDLHelper.GetExceptionMessage(title));
                return dt;
            });
            tskh.ContinueWith(() =>
            {
                succussAct(tskh.Task.Result);
            });
            tskh.Start();
        }
        public static DataTable SelectTable(string title, string connName, string tblName, string search, string fields = "*")
        {
            DataTable dt = WSDLHelper.InvokeMethod<DataTable>("SelectTable", new object[] { connName, tblName, search, fields });
            if (dt == null) MessageBox.Show(WSDLHelper.GetExceptionMessage(title));
            return dt;
        }

        public static bool UpdateRecords(string title, string connName, string tblName, string fields, string search, bool showMsg = false)
        {
            string msg = WSDLHelper.InvokeMethod<string>("UpdateRecords", new object[] { connName, tblName, fields, search });
            if (msg == null)
            {
                WSDLHelper.ShowMessage(title);
                return false;
            }
            if (showMsg) MessageBox.Show(msg.ToBoolean() ? title + "成功!" : msg);
            return msg.ToBoolean();


        }
    }
}
